package com.petcare;

import com.bd.PetsModule;
import com.bd.Utilities;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class newpet extends Activity {

	private static final int SELECT_PHOTO = 100;
	private static String filePath = "";
	ImageView pic;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_newpet);
	    
	    final EditText tname = (EditText) findViewById(R.id.editText1);
        final EditText tage = (EditText) findViewById(R.id.editText2);
        final EditText tanimaltype = (EditText) findViewById(R.id.editText3);
        final EditText trace = (EditText) findViewById(R.id.editText4);
        final EditText tweight = (EditText) findViewById(R.id.editText5);
        final EditText theight = (EditText) findViewById(R.id.editText6);
        final EditText tfood = (EditText) findViewById(R.id.editText7);
        final EditText tvitamins = (EditText) findViewById(R.id.editText8);
        
        
	    
	    final Button button1 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View v) {
            	Intent myIntent = new Intent(newpet.this, newalarm.class);
            	startActivity(myIntent);
            }
        });
        
        final Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View v) {
            	Intent myIntent = new Intent(newpet.this, mainMenu.class);
            	startActivity(myIntent);
            }
        });
        
        //Guardar
        Button bsave = (Button) findViewById(R.id.button3);
        bsave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String nombre = tname.getText().toString();
				String tipo = tanimaltype.getText().toString();
				String raza = trace.getText().toString();
				String peso = tweight.getText().toString();
				String altura = theight.getText().toString();
				String comida = tfood.getText().toString();
				String vitaminas = tvitamins.getText().toString();
				
				String edad = tage.getText().toString();
				PetsModule nuevo = new PetsModule(nombre, edad, filePath, tipo, raza, peso, altura, comida, vitaminas);
				Utilities.datasource.createPet(nuevo);
				Toast.makeText(getBaseContext(), "Pet created!", Toast.LENGTH_SHORT).show();
			}
			
		}
        );
	
        Button bpicture = (Button) findViewById(R.id.button1);
        bpicture.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
				photoPickerIntent.setType("image/*");
				startActivityForResult(photoPickerIntent, SELECT_PHOTO);  
			}
		});
	    // TODO Auto-generated method stub
        Button bprint = (Button) findViewById(R.id.button5);
        bprint.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String algo = Utilities.datasource.getAllPets().toString();
				Log.v("PRINT", algo);
			}
		});
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
	
	protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
	    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

	    switch(requestCode) { 
	    case SELECT_PHOTO:
	        if(resultCode == RESULT_OK){  
	            Uri selectedImage = imageReturnedIntent.getData();
	            String[] filePathColumn = {MediaStore.Images.Media.DATA};

	            Cursor cursor = getContentResolver().query(
	                               selectedImage, filePathColumn, null, null, null);
	            cursor.moveToFirst();

	            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	            filePath = cursor.getString(columnIndex);
	            cursor.close();
	            
	            //Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
	            //pic.setImageBitmap(yourSelectedImage);
	        }
	    }
	}

}
